//
//  KDWindow.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 16/2/23.
//  Copyright © 2016年 魏强. All rights reserved.
//

#import "KDWindow.h"
@interface KDWindow ()
{
    BOOL contain;
    CGPoint startPoint;//按钮每动一次的中心点
    CGPoint originPoint;//按钮的初始中心点
}
@property (nonatomic, weak) UIView *underView;

@end
@implementation KDWindow

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)makeKeyAndVisible{
    [super makeKeyAndVisible];
    [self setUpBackMainPageBtn];
}

- (void)showMainPageBtn{
    self.mainPageBtn.hidden = NO;
    [self bringSubviewToFront:self.underView];

}

- (void) setUpBackMainPageBtn{
    
    // 用来装btn的View，主要用于触发长按效果
    UIView *view = [UIView new];
    view.frame = CGRectMake(30, screenH - 50, 50, 50);
    view.backgroundColor = [UIColor clearColor];
    self.underView = view;
    [self addSubview:view];
    
    UIButton *MainPageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    MainPageBtn.frame = view.bounds;
//    [MainPageBtn setTitle:@"回主" forState:UIControlStateNormal];
    [MainPageBtn setImage:[UIImage imageNamed:@"MainPageBtn_nomal"] forState:UIControlStateNormal];
    [MainPageBtn setImage:[UIImage imageNamed:@"MainPageBtn_heightlight"] forState:UIControlStateHighlighted];

    MainPageBtn.layer.cornerRadius = view.width *0.5;
    MainPageBtn.alpha = 1.0;
    [MainPageBtn clipsToBounds];
    [MainPageBtn addTarget:self action:@selector(MainPageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    MainPageBtn.backgroundColor = RandomColor;
    [MainPageBtn setTitleColor:KDColor forState:UIControlStateNormal];;
    self.mainPageBtn = MainPageBtn;
    MainPageBtn.hidden = YES;
    [view addSubview:MainPageBtn];
    
    UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    longpress.minimumPressDuration = 0.10;//定义长按时间
    [view addGestureRecognizer:longpress];
}
- (void)MainPageBtnClick:(UIButton *)MainPageBtn{
    [self.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
/**
 *  长按后触发各种变化
 */
- (void)longPress:(UILongPressGestureRecognizer *)longpress{
    UIView*btn = (UIView *)longpress.view;
    
    if ([longpress state] == UIGestureRecognizerStateBegan) {
        startPoint = [longpress locationInView:btn];
        originPoint = btn.center;
        [UIView animateWithDuration:0.5 animations:^{//改变btn的大小
            btn.transform = CGAffineTransformMakeScale(1.5, 1.5);
            btn.alpha = 0.7;
                [_mainPageBtn setImage:[UIImage imageNamed:@"MainPageBtn_heightlight"] forState:UIControlStateNormal];
        }];
    }else if ([longpress state] == UIGestureRecognizerStateChanged){
        CGPoint newPoint = [longpress locationInView:btn];
        CGFloat deltaX = newPoint.x - startPoint.x;
        CGFloat deltaY = newPoint.y - startPoint.y;
        btn.center = CGPointMake(btn.centerX + deltaX, btn.centerY +deltaY);//让触摸点始终处于同一位置
    }else if ([longpress state] == UIGestureRecognizerStateEnded){
        [UIView animateWithDuration:0.3 animations:^{
            btn.transform = CGAffineTransformIdentity;
             [_mainPageBtn setImage:[UIImage imageNamed:@"MainPageBtn_nomal"] forState:UIControlStateNormal];
//            btn.alpha = 1.0;
//            if (!contain) {
//                btn.center = originPoint;
//            }
        }];
    }
}
@end
